/*
 * adc3200.c
 *
 * Created: 10/18/2012 9:57:24 PM
 *  Author: Evan
 */ 

#include "adc3200.h"
#include <avr/io.h>

/***************************************************************
 * adc_init
 * default init: AVCC, clk/128, ADC on
 * does not start a conversion
 **************************************************************/
void adc_init(unsigned char pin){
	DDRA = 0x00; // portA all inputs
    ADMUX = (1<<REFS0); // AVCC as reference
	if (pin<8) 
		ADMUX = ADMUX | pin; // set the ADC pin

	// enable ADC, set clk/128
    ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
}

/***************************************************************
 * adc_start
 * start an ADC conversion
 * does not verify ADC is initialized
 **************************************************************/
void adc_start(void){
	ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) | (1<<ADSC);
}

/***************************************************************
 * adc_wait
 * wait for a conversion to finish
 **************************************************************/
void adc_wait(void){
   while((ADCSRA & (1<<ADSC)));
}

/***************************************************************
 * get_adch
 * returns the contents of ADCH
 **************************************************************/
unsigned char get_adch(void){
   return ADCH; 
}

/***************************************************************
 * get_adcl
 * returns the contents of ADCL
 **************************************************************/
unsigned char get_adcl(void){
   return ADCL;
}

/***************************************************************
 * get_adc
 * returns the contents of ADC
 **************************************************************/
unsigned int get_adc(void){
	return ADC;
}
